<template>
  <el-container class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside width="250px" class="sidebar">
      <div class="sidebar-header">
        <h2>后台管理</h2>
      </div>

      <el-menu :default-active="activeMenu" class="sidebar-menu" router background-color="#304156" text-color="#bfcbd9"
        active-text-color="#409eff">
        <el-menu-item index="/admin">
          <el-icon>
            <DataBoard />
          </el-icon>
          <span>仪表盘</span>
        </el-menu-item>

        <el-menu-item index="/admin/users">
          <el-icon>
            <User />
          </el-icon>
          <span>用户管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/products">
          <el-icon>
            <Goods />
          </el-icon>
          <span>商品管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/orders">
          <el-icon>
            <List />
          </el-icon>
          <span>订单管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/categories">
          <el-icon>
            <Menu />
          </el-icon>
          <span>分类管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/messages">
          <el-icon>
            <Message />
          </el-icon>
          <span>消息管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/actions">
          <el-icon>
            <Star />
          </el-icon>
          <span>互动管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/comments">
          <el-icon>
            <ChatDotRound />
          </el-icon>
          <span>评论管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主要内容区域 -->
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="admin-header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin' }">后台管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="admin-user">
              <el-avatar :size="32" :src="userStore.user?.avatar">
                {{ userStore.user?.username?.charAt(0)?.toUpperCase() }}
              </el-avatar>
              <span class="username">{{ userStore.user?.username }}</span>
              <el-icon>
                <ArrowDown />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="frontend">返回前台</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 内容区域 -->
      <el-main class="admin-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  DataBoard,
  User,
  Goods,
  List,
  Menu,
  Message,
  Star,
  ChatDotRound,
  ArrowDown
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

// 计算属性
const activeMenu = computed(() => route.path)
const currentPageTitle = computed(() => {
  const routeMap: Record<string, string> = {
    '/admin': '仪表盘',
    '/admin/users': '用户管理',
    '/admin/products': '商品管理',
    '/admin/orders': '订单管理',
    '/admin/categories': '分类管理',
    '/admin/messages': '消息管理',
    '/admin/actions': '互动管理',
    '/admin/comments': '评论管理'
  }
  return routeMap[route.path] || '未知页面'
})

// 处理下拉菜单命令
const handleCommand = async (command: string) => {
  switch (command) {
    case 'frontend':
      router.push('/')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        userStore.logout()
        router.push('/login')
        ElMessage.success('已退出登录')
      } catch {
        // 用户取消
      }
      break
  }
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

/* 侧边栏样式 */
.sidebar {
  background-color: #304156;
  color: #bfcbd9;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #1f2d3d;
}

.sidebar-header h2 {
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  margin: 0;
}

.sidebar-menu {
  border: none;
}

.sidebar-menu .el-menu-item {
  height: 50px;
  line-height: 50px;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #263445 !important;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: #1890ff !important;
}

/* 顶部导航栏样式 */
.admin-header {
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.admin-user {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.admin-user:hover {
  background-color: #f5f7fa;
}

.username {
  font-size: 14px;
  color: #606266;
}

/* 主要内容区域样式 */
.admin-main {
  background-color: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
}
</style>